﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;

namespace RoleBasedSecurity
{
    public partial class RoleManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var dv = Roles.GetRoles().DefaultView;
            dv.Sort = "Rolename";
            ListBox1.DataSource = dv;
            ListBox1.DataTextField = "Rolename";
            ListBox1.DataValueField = "Rolecode";
            ListBox1.DataBind();
            if (!IsPostBack)
            {
                var dv1 = Members.GetAllUsers().DefaultView;
                dv1.Sort = "Firstname";
                listUsers.DataSource = dv1;
                listUsers.DataTextField = "Firstname";
                listUsers.DataValueField = "UserID";
                listUsers.DataBind();
            }            
        }

        protected void btnAddRole_Click(object sender, EventArgs e)
        {
            Roles.AddRole(txtRolename.Text, txtRolecode.Text, txtroleDesc.Text);
            Response.Redirect("~/RoleManager.aspx");
        }

        protected void listUsers_SelectedIndexChanged(object sender, EventArgs e)
        {

            assignedRoles.Items.Clear();
            unAssignedRoles.Items.Clear();

            assignedRoles.DataSource = Roles.GetRoleByUserID(listUsers.SelectedValue);
            assignedRoles.DataTextField = "Rolename";
            assignedRoles.DataValueField = "RoleID";
            assignedRoles.DataBind();

            var dt1 = from row in Roles.GetRoles().AsEnumerable() select row;
            var dt2 = from row in Roles.GetRoleByUserID(listUsers.SelectedValue).AsEnumerable() select row;
            var result = dt1.Except(dt2, DataRowComparer.Default);

            unAssignedRoles.DataSource = result;
            unAssignedRoles.DataTextField = "Rolename";
            unAssignedRoles.DataValueField = "RoleID";
            unAssignedRoles.DataBind();
        }

        protected void assignedRoles_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnMove_Click(object sender, EventArgs e)
        {
            var selectedIndices = assignedRoles.GetSelectedIndices();
            ListItemCollection selectedItems = new ListItemCollection();
            foreach (var i in selectedIndices)
            {
                selectedItems.Add(assignedRoles.Items[i]);
            }

            foreach (ListItem item in selectedItems)
            {
                unAssignedRoles.Items.Add(item);
                assignedRoles.Items.Remove(item);
                MemberRoles.RemoveRoles(Guid.Parse(listUsers.SelectedValue), Guid.Parse(item.Value));
            }
            unAssignedRoles.ClearSelection();
        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            var selectedIndices = unAssignedRoles.GetSelectedIndices();
            ListItemCollection selectedItems = new ListItemCollection();
            foreach (var i in selectedIndices)
            {
                selectedItems.Add(unAssignedRoles.Items[i]);
            }

            foreach (ListItem item in selectedItems)
            {
                assignedRoles.Items.Add(item);
                unAssignedRoles.Items.Remove(item);
                MemberRoles.AddMemberRoles(Guid.Parse(listUsers.SelectedValue), Guid.Parse(item.Value));
            }
            assignedRoles.ClearSelection();
        }
    }
}